home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / boot / edit.pl < prev    next >
Encoding:
Text File  |  1997-03-10  |  5.2 KB  |  202 lines

  1. /*  $Id: edit.pl,v 1.13 1997/03/10 09:36:08 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: editor interface
  7. */
  8.  
  9. :- module($edit,
  10.     [ edit/1
  11.     , edit/0
  12.     , ed/1
  13.     , ed/0
  14.     , make/0
  15.     ]).
  16.  
  17. :- dynamic
  18.     user:edit_source/1.
  19. :- multifile
  20.     user:edit_source/1.
  21.  
  22. :- module_transparent
  23.     edit/1, 
  24.     ed/1.
  25.  
  26. edit(File) :-
  27.     $check_file(File, Path),
  28.     \+ qlf_file(Path), !,
  29.     $record_last($edit_file, File), 
  30.     $edit_load(File).
  31. edit(File) :-
  32.     $confirm('No such file.  Edit new file ~w', [File]),
  33.     $record_last($edit_file, File), 
  34.     $edit(File).
  35.  
  36. edit :-
  37.     recorded($edit_file, File), 
  38.     $confirm('Edit file `~w''', [File]), !,
  39.     edit(File).
  40.  
  41. ed(Spec) :-
  42.     $find_predicate(Spec, Preds),
  43.     (   Preds = [Head]
  44.     ;   member(Head, Preds), 
  45.         $predicate_name(Head, PredName),
  46.         $confirm('Edit predicate `~w''', [PredName])
  47.     ), !, 
  48.     $define_predicate(Head),
  49.     source_file(Head, File), 
  50.     predicate_property(Head, line_count(LineNo)),
  51.     $strip_module(Head, Module, Term), 
  52.     functor(Term, Name, Arity), 
  53.     $record_last($edit_predicate, Module:Name/Arity), 
  54.     $edit_load(File:LineNo:Name/Arity).
  55.  
  56. ed :-
  57.     $module(TypeIn, TypeIn), 
  58.     recorded($edit_predicate, TypeIn:Name/Arity), !, 
  59.     $confirm('Edit predicate `~w/~w''', [Name, Arity]), !, 
  60.     ed(TypeIn:Name/Arity).
  61. ed :-
  62.     recorded($edit_predicate, Module:Name/Arity), !, 
  63.     $confirm('Edit predicate `~w:~w/~w''', [Module, Name, Arity]), !, 
  64.     ed(Module:Name/Arity).
  65. ed :-
  66.     $warning('ed/0: You can only use ed/0 after ed/1!').
  67.  
  68. qlf_file(Path) :-
  69.     concat(_, '.qlf', Path), !.
  70.  
  71. $record_last(Key, Term) :-
  72.     recorded(Key, Last) -> 
  73.     Last = Term, !.
  74. $record_last(Key, Term) :-
  75.     recorda(Key, Term).
  76.  
  77. $edit_load(File:Predicate) :-
  78.     (   $check_file(File, Path), 
  79.         \+ qlf_file(Path)
  80.     ->  $edit(Path:Predicate), !
  81.     ).
  82. $edit_load(File) :-
  83.     File \= _:_,
  84.     (   $check_file(File, Path), 
  85.         \+ qlf_file(Path)
  86.     ->  $edit(Path), !
  87.     ).
  88.  
  89. $edit(Spec) :-
  90.     user:edit_source(Spec), !.
  91. $edit(File:LineNo:Name/_Arity) :- !,
  92.     (   (   getenv('EDITOR', Editor)
  93.         ;   $default_editor(Editor)
  94.         )
  95.     ->  (    (   edit_command(Editor, File, LineNo, Name, Command)
  96.         ;   edit_command(Editor, File, LineNo, $nopredicate, Command)
  97.         )
  98.         ->  shell(Command),
  99.         make
  100.         )
  101.     ).
  102. $edit(File) :-
  103.     (   (   getenv('EDITOR', Editor)
  104.         ;   $default_editor(Editor)
  105.         )
  106.     ->  edit_command(Editor, File, 1, $nopredicate, Command),
  107.         shell(Command),
  108.         make
  109.     ).
  110.  
  111. edit_command(Editor, File, _Line, $nopredicate, Command) :- !,
  112.     prolog_to_os_filename(File, OsFile),
  113.     file_base_name(Editor, Base),
  114.     (   edit_command(Base, nosearch, Cmd)
  115.     ->  name(Cmd, S0),
  116.         substitute("%e", Editor, S0, S1),
  117.         substitute("%f", OsFile, S1, S),
  118.         name(Command, S)
  119.     ;   $warning('Don''t know how to use editor `~w''', [Editor])
  120.     ).
  121. edit_command(Editor, File, Line, Pred, Command) :-
  122.     prolog_to_os_filename(File, OsFile),
  123.     file_base_name(Editor, Base),
  124.     (   edit_command(Base, search, Cmd)
  125.     ->  name(Cmd, S0),
  126.         substitute("%e", Editor, S0, S1),
  127.         substitute("%f", OsFile, S1, S2),
  128.         substitute("%s", Pred, S2, S3),
  129.         substitute("%d", Line, S3, S),
  130.         name(Command, S)
  131.     ).
  132.  
  133. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  134. edit_command(+Editor, +search, -Command)
  135.  
  136. This predicate should specify the  shell-command   called  to invoke the
  137. user's editor.  The following substitutions will be made:
  138.  
  139.     %e        Path name of the editor
  140.     %f        Path name of the file to be edited
  141.     %s        Name of the predicate (only for `search')
  142.     %d        Line number of the predicate (only for `search')
  143.  
  144. To locate a predicate in  a   source-file,  two mechanisms are provided.
  145. The first will *search* for the   predicate definition, while the second
  146. uses the *line-number* info from the Prolog system.  Searching generally
  147. only handles finding the  definition  of   the  first  occurrence of the
  148. predicate, disregarding the arity information.    Using  line-numbers is
  149. not sensitive to changes.  Smart editors  may pass both informations and
  150. search nearby the given line number.
  151. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  152.  
  153. edit_command(top,          search,   '%e ''%f'' ''-^%s''').
  154. edit_command(vi,        search,   '%e ''+/^%s'' ''%f''').
  155. edit_command(emacs,           search,   '%e +%d ''%f''').
  156. edit_command(emacsclient,       search,   '%e +%d ''%f''').
  157. edit_command(notepad,         nosearch, '"%e" "%f"').
  158. edit_command(_,               nosearch, '"%e" "%f"').
  159.  
  160. substitute(From, ToAtom, Old, New) :-
  161.     name(ToAtom, To),
  162.     append(Pre, S0, Old),
  163.     append(From, Post, S0) ->
  164.     append(Pre, To, S1),
  165.     append(S1, Post, New), !.
  166. substitute(_, _, Old, Old).
  167.  
  168.  
  169.         /********************************
  170.         *              MAKE             *
  171.         *********************************/
  172.  
  173. %    make
  174. %    Reload all source files that have been changed since they were
  175. %    loaded.
  176.  
  177. make :-
  178.     $update_library_index,
  179.     $time_source_file(File, LoadTime),
  180.     time_file(File, Modified),
  181.     Modified @> LoadTime,
  182.     reload(File),
  183.     fail.
  184. make.
  185.  
  186.  
  187. %    reload(File)
  188. %
  189. %    Reload file into the proper module.  Note that if the file is loaded
  190. %    into multiple modules this should be handled more carefully.
  191.  
  192. reload(File) :-
  193.     findall(Context, $load_context_module(File, Context), Modules),
  194.     (   Modules = []
  195.     ->  consult(user:File)
  196.     ;   Modules = [Module]
  197.     ->  consult(Module:File)
  198.     ;   Modules = [First|_Rest],
  199.         consult(First:File)
  200.     ).
  201.     
  202.